home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2347 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.2 KB  |  149 lines

  1. Newsgroups: comp.lang.c
  2. Path: gail.ripco.com!mambuhl
  3. From: mambuhl@ripco.com (Martin Ambuhl)
  4. Subject: Re: Please help ?!       
  5. X-Nntp-Posting-Host: foley.ripco.com
  6. Message-ID: <DLHv58.Cn1@rci.ripco.com>
  7. Sender: usenet@rci.ripco.com (Net News Admin)
  8. Organization: Ripco Internet BBS Chicago
  9. Date: Sat, 20 Jan 1996 19:22:19 GMT
  10. X-Ident-Sender: mambuhl
  11.  
  12. mv@pi.net
  13. in <4dm889$3hs@neptunus.pi.net> asks:
  14.  
  15. >Hello everybody,
  16.  
  17.  
  18. >Please take a look at this:
  19.  
  20. [snip]
  21.  
  22. >/*
  23.  
  24. > Here it should print:
  25.  
  26. >Switch: /w:test
  27. >Switch: /1
  28. >etc..
  29.  
  30. >but it prints:
  31.  
  32. >Switch: /w:test
  33. >Switch:
  34. >Switch: /1
  35. >and so on....
  36. >It jjust skips one array index everytime... It seems to go wrong in get_switche
  37. >()...
  38. >*/
  39.  
  40. Martijn,
  41.     Your code is logically a mess, but should also have never compiled.
  42.     I have not changed the logic of your code, but have cleaned it up
  43.     enough so that it will compile and run.  Turn your diagnostics back
  44.     on.  The compiler issues them for a reason.  The cleaned up code is
  45.     below, with my changes marked with "/* mha ... */".  You still need
  46.     to clean up the logic, even though it works as is. I don't see how
  47.     your code ever did anything as you posted it.
  48.  
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #if 0
  53. #include <conio.h>              /* mha - non-standard header serving no
  54.                                  * real purpose in this code */
  55. #include <alloc.h>              /* mha - non-standard header which
  56.                                  * _never_ serves any purpose */
  57. #endif
  58.  
  59. #define MAXLEN    20
  60. #define MAXSWITCH 20
  61.  
  62. char *s = "dir /w:test/1/2/3/4";
  63. char *s2, *s3;
  64. char myswitches[MAXSWITCH][MAXLEN];
  65.  
  66. int get_switches(char *cmd, char switches[][MAXLEN]
  67.                   /* mha - was "*switches[][MAXLEN]" */ )
  68. {
  69.     int i = 0;
  70.     char *cpy;
  71.     char *saved;
  72.  
  73.     if (strlen(cmd) == 0) {
  74.         return 0;
  75.     }
  76.     cpy = malloc(MAXLEN);       /* mha - removed possibly error-masking
  77.                                  * and certainly useless cast (char *) */
  78.     if (cpy == NULL) {
  79.         printf("Out of memory in %s on line: %d\n", __FILE__, __LINE__);
  80.         exit(EXIT_FAILURE);
  81.     }
  82.     *cpy = '\0';                /* mha - replaced illegal and wasteful
  83.                                  * "strcpy(cpy, '\0');" */
  84.     saved = cpy;
  85.     while (*cmd != '\0') {
  86.         if (*cmd == '/') {
  87.             if (i == MAXSWITCH) {
  88.                 printf("INT: Too many switches ...\n");
  89.                 return 9999;
  90.             }
  91.             *cpy = *cmd;
  92.             do {
  93.                 *cpy++ = *cmd++;
  94.             } while (!strchr(" /-\0", *cmd));
  95.             *cpy = '\0';
  96.             cpy = saved;
  97.             strcpy(switches[i], cpy);
  98.             *cpy = '\0';        /* mha - replaced illegal and wasteful
  99.                                  * "strcpy(cpy, '\0');" */
  100.             i++;
  101.         }
  102.         if (*cmd == '-') {
  103.             if (i == MAXSWITCH) {
  104.                 printf("INT: Too many switches ...\n");
  105.                 return 9999;
  106.             }
  107.             *cpy = *cmd;
  108.             do {
  109.                 *cpy++ = *cmd++;
  110.             } while (!strchr(" /-\0", *cmd));
  111.             *cpy = '\0';
  112.             cpy = saved;
  113.             strcpy(switches[i], cpy);
  114.             *cpy = '\0';        /* mha - replaced illegal and wasteful
  115.                                  * "strcpy(cpy, '\0');" */
  116.             i++;
  117.         }
  118.         if (!strchr("/-\0", *cmd)) {
  119.             *cmd++;             /* mha - what is this supposed to be?
  120.                                  * I'm sure this does _not_ do whatever you
  121.                                  * intended! (No change made, since the intent
  122.                                  * is no clearer to me than to your compiler) */
  123.         }
  124.     }
  125.     free(cpy);
  126.     return i;
  127. }
  128.  
  129.  
  130. int main(void)
  131. {
  132.     int i;
  133. #if 0
  134.     clrscr();                   /* mha - removed this pointless
  135.                                  * non-standard function call */
  136. #endif
  137.     i = get_switches(s, myswitches);
  138.     printf("Switches found: %i\n", i);
  139.     for (i = 0; i < MAXSWITCH; i++) {
  140.         printf("Switch: %s\n", myswitches[i]);
  141.     }
  142.     return 0;
  143. }
  144.  
  145.                                                                                      
  146. --
  147. * Martin Ambuhl       net: mambuhl@ripco.com
  148. * Chicago, IL (USA)    
  149.